﻿using System;
using System.Text; 
using System.Collections.Generic;
using System.Data.SqlClient;

namespace Orid.Management.Statistics.Tools.Decomposition.Data
{
    class JobLog : DataBase
    {
        public virtual List<Orid.Management.Statistics.Tools.Decomposition.Model.ModelJobLog>  GetJobLog(Guid JobID,int downloadID)
        {
            return null;
        }

        protected List<Orid.Management.Statistics.Tools.Decomposition.Model.ModelJobLog> GetJobLog(Guid JobID, int downloadID,string connectString, string jobTableName, Account account)
        {
            if (JobID == null)
            {
                return null;
            }

            List<Orid.Management.Statistics.Tools.Decomposition.Model.ModelJobLog> Model = new List<Orid.Management.Statistics.Tools.Decomposition.Model.ModelJobLog>();


            StringBuilder sqlText = new StringBuilder();
            sqlText.Append(" SELECT  [DateTime] ,[AccountID]  ,[JobID]   ,[OrderID] ");     
            sqlText.Append(" FROM " + jobTableName + "  [Joblogs ]   with(NOLOCK)");//readpast
            sqlText.Append(" WHERE     [JobID] = @JobID AND [Action] = 2 AND [DownloadID] =@DownloadID ");

            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
            {

                conn.Open();
                System.Data.SqlClient.SqlParameter[] SqlParameter;
                SqlParameter = new System.Data.SqlClient.SqlParameter[]
                                 {   new System.Data.SqlClient.SqlParameter("@JobID",System.Data.SqlDbType.UniqueIdentifier),                                
                                     new System.Data.SqlClient.SqlParameter("@DownloadID",System.Data.SqlDbType.Int),                                

                                 };
                SqlParameter[0].Value = JobID;
                SqlParameter[1].Value = downloadID;

                Orid.Data.SqlServer.SqlHelper.TimeOut = msTimeOut;
                SqlDataReader joblogReader = Orid.Data.SqlServer.SqlHelper.ExecuteReader(conn, System.Data.CommandType.Text, sqlText.ToString(), SqlParameter);
                Model = InitJobLogModel(joblogReader, account);
                if (conn.State == System.Data.ConnectionState.Open)
                    conn.Close();



            }

            return Model;
        }

        private List<Orid.Management.Statistics.Tools.Decomposition.Model.ModelJobLog> InitJobLogModel(SqlDataReader reader, Account account)
        {
            List<Orid.Management.Statistics.Tools.Decomposition.Model.ModelJobLog> item = new List<Orid.Management.Statistics.Tools.Decomposition.Model.ModelJobLog>();

            try
            {
                while (reader.Read())
                {
                    Orid.Management.Statistics.Tools.Decomposition.Model.ModelJobLog log = new Orid.Management.Statistics.Tools.Decomposition.Model.ModelJobLog();

                    if (reader["DateTime"] != DBNull.Value)
                    {
                        log.CancelDatetime =  (DateTime)reader["DateTime"];
                    }

                    if (reader["AccountID"] != DBNull.Value)
                    {
                        int accountID = (int)reader["AccountID"];
                        log.LoginName = account.GetLoginName(accountID);

                    }
                    if (reader["JobID"] != DBNull.Value)
                    {
                        log.JobID = (Guid)reader["JobID"];
                    }
                    if (reader["OrderID"] != DBNull.Value)
                    {
                        log.OrderID = (Guid)reader["OrderID"];
                    }
                    item.Add(log);
                   
                  
                  
                }
                reader.Close();
            }
             catch(Exception ex)
            {
                 throw ex ;
             }
            return item;
            
        }

    }
}
